<?php
global $socket;

switch ($aEvent->type)
{
	case Module::INITIALIZE_MODULE:
		if (!class_exists('Message'))
		{
			$definition =  'class Message
							{
								const SEND = "<Message>: Send message";
								const PARSE = "<Message>: Parse message";
								const INIT_USER = "<Message>: Init user";
								const INIT_NICK = "<Message>: Init nick";
								const SENDPONG = "<Message>: Send PONG";
								const JOINCHANNELS = "<Message>: Join channels";
							}';
							
			eval($definition);
							
			Events::addEventListener(Message::SEND, "ircmessaging");
			Events::addEventListener(Message::PARSE, "ircmessaging");
			Events::addEventListener(Message::INIT_USER, "ircmessaging");
			Events::addEventListener(Message::INIT_NICK, "ircmessaging");
			Events::addEventListener(Message::SENDPONG, "ircmessaging");
			Events::addEventListener(Message::JOINCHANNELS, "ircmessaging");
			Events::addEventListener(MessageEvent::MESSAGE, "ircmessaging");
		}
		break;
		
	case Message::SEND:
		fputs($socket, $aEvent->data . chr(13) . chr(10));
		Debug::log("Sent message event: [" . $aEvent->data . "]", Debug::INFO);
		break;
		
	case Message::PARSE:
		$unparsedMessageString = $aEvent->data;
		$parsedMessageString = $unparsedMessageString;
		
		$prefix = null;
		$command = null;
		$target = null;
		$paramstring = null;
		$params = array();
		
		// Find prefix
		if (substr($parsedMessageString, 0, 1) == Strings::PART_SEPERATOR)
		{
			$nextSpace = strpos($parsedMessageString, chr(32));
			$prefix = substr($parsedMessageString, 0, $nextSpace);
			$parsedMessageString = substr($parsedMessageString, $nextSpace + 1, strlen($parsedMessageString) - $nextSpace);
		}
		
		// Find command
		$nextSpace = strpos($parsedMessageString, chr(32));
		$command = substr($parsedMessageString, 0, $nextSpace);
		$parsedMessageString = substr($parsedMessageString, $nextSpace + 1, strlen($parsedMessageString) - $nextSpace);
		
		// Find target
		if (substr($parsedMessageString, 0, 1) != Strings::PART_SEPERATOR)
		{
			$nextSpace = strpos($parsedMessageString, chr(32));
			$target = substr($parsedMessageString, 0, $nextSpace);
			$parsedMessageString = substr($parsedMessageString, $nextSpace + 1, strlen($parsedMessageString) - $nextSpace);
		}
		
		// Find params
		$paramString = $parsedMessageString;
		if (strrpos($paramString, chr(32) . Strings::PART_SEPERATOR) > 0)
			$params = explode(chr(32) . Strings::PART_SEPERATOR, $paramString);
		else
			$params = array($paramString);
		
		for ($i = 0; $i < count($params); $i++)
		{
			$params[$i] = trim($params[$i]);
			
			if (substr($params[$i], 0, 1) == Strings::PART_SEPERATOR)
				$params[$i] = substr($params[$i], 1, strlen($params[$i]) - 1);
		}
		
		if (isset($command) && $command != "")
			Events::dispatchEvent(new MessageEvent(MessageEvent::MESSAGE, $unparsedMessageString, $prefix, $command, $target, $params));
		break;
		
	case MessageEvent::MESSAGE:
		Debug::log("Received message event: " . $aEvent->toString(), Debug::INFO);
		
		if ($aEvent->to == Configuration::get("nickname") &&
			$aEvent->command == 255)
			Events::dispatchEvent(new Event(Message::JOINCHANNELS));
		break;
		
	case Message::INIT_USER:
		Events::dispatchEvent(new Event(Message::SEND, "USER " . Configuration::get("nickname") . " " . Configuration::get("hostname") . " " . Configuration::get("hostname") . " : " . Configuration::get("fullname")));
		break;

	case Message::INIT_NICK:
		Events::dispatchEvent(new Event(Message::SEND, "NICK " . Configuration::get("nickname") . " " . Configuration::get("hostname")));
		break;
		
	case Message::SENDPONG:
		Events::dispatchEvent(new Event(Message::SEND, "PONG " . $aEvent->data));
		break;
		
	case Message::JOINCHANNELS:
		$theConfiguredChannels = Configuration::get("channels");
		if (trim($theConfiguredChannels) != "")
			Events::dispatchEvent(new Event(Message::SEND, "JOIN " . trim($theConfiguredChannels)));
		else
			Debug::log("Your bot doesn't have any proper channels configured, check your configuration", Debug::WARNING);
		break;
		
	case Module::UNLOAD_MODULE:
		Events::removeEventListener(Message::SEND, "ircmessaging");
		Events::removeEventListener(Message::PARSE, "ircmessaging");
		Events::removeEventListener(Message::INIT_USER, "ircmessaging");
		Events::removeEventListener(Message::INIT_NICK, "ircmessaging");
		Events::removeEventListener(Message::SENDPONG, "ircmessaging");
		Events::removeEventListener(Message::JOINCHANNELS, "ircmessaging");
		Events::removeEventListener(MessageEvent::MESSAGE, "ircmessaging");
		break;
	
	default:
		break;
}
?>